package com.healthcloud.controller.modules.patient;

import com.healthcloud.controller.modules.common.BaseController;
import com.healthcloud.entity.common.PageList;
import com.healthcloud.entity.patient.PatientDataEntity;
import com.healthcloud.entity.screening.*;
import com.healthcloud.service.patient.PatientDataService;
import com.healthcloud.service.screening.ScreeningBlService;
import com.healthcloud.service.screening.ScreeningBpService;
import com.healthcloud.service.screening.ScreeningBsService;
import com.healthcloud.utils.ApiRes;
import com.healthcloud.utils.PageParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 病人筛查
 * 
 */
@Controller
@RequestMapping("screening")
public class ScreeningController extends BaseController {

    @Autowired
    private ScreeningBsService screeningBsService;

    @Autowired
    private ScreeningBpService screeningBpService;

    @Autowired
    private ScreeningBlService screeningBlService;

    @Autowired
    private PatientDataService patientDataService;

    @ApiOperation(value = "血压筛查病人列表")
    @RequestMapping(value = "/bpPatients", method = RequestMethod.GET)
    public String bpPatients(){
        setAttr();
        return "bus/screening/bpPatients";
    }

    @ApiOperation(value = "血压筛查列表")
    @RequestMapping(value = "/bpScreenings", method = RequestMethod.GET)
    public String bpScreenings(){
        setAttr();
        return "bus/screening/bpScreenings";
    }

    @ApiOperation(value = "血压查看")
    @RequestMapping(value = "/bpScreening", method = RequestMethod.GET)
    public String bpScreening(){
        setAttr();
        return "bus/screening/bpScreening";
    }

    @ApiOperation(value = "血糖筛查病人列表")
    @RequestMapping(value = "/bsPatients", method = RequestMethod.GET)
    public String bsPatients(){
        setAttr();
        return "bus/screening/bsPatients";
    }

    @ApiOperation(value = "血糖筛查列表")
    @RequestMapping(value = "/bsScreenings", method = RequestMethod.GET)
    public String bsScreenings(){
        setAttr();
        return "bus/screening/bsScreenings";
    }

    @ApiOperation(value = "血糖查看")
    @RequestMapping(value = "/bsScreening", method = RequestMethod.GET)
    public String bsScreening(){
        setAttr();
        return "bus/screening/bsScreening";
    }

    @ApiOperation(value = "骨松筛查病人列表")
    @RequestMapping(value = "/blPatients", method = RequestMethod.GET)
    public String blPatients(){
        setAttr();
        return "bus/screening/blPatients";
    }

    @ApiOperation(value = "骨松筛查列表")
    @RequestMapping(value = "/blScreenings", method = RequestMethod.GET)
    public String blScreenings(){
        setAttr();
        return "bus/screening/blScreenings";
    }

    @ApiOperation(value = "骨松查看")
    @RequestMapping(value = "/blScreening", method = RequestMethod.GET)
    public String blScreening(){
        setAttr();
        return "bus/screening/blScreening";
    }

    @ApiOperation(value = "血压筛查列表")
    @RequestMapping(value = "/bpList", method = RequestMethod.POST)
    @ResponseBody
    public PageList bpList(ScreeningBpVo entity, PageParam pageParam){
        List<ScreeningBpVo> bps = screeningBpService.queryListVo(entity, pageParam);
        Integer count = screeningBpService.queryTotalVo(entity);
        return new PageList(bps, count, count, pageParam.getsEcho());
    }

    @ApiOperation(value = "血糖筛查列表")
    @RequestMapping(value = "/bsList", method = RequestMethod.POST)
    @ResponseBody
    public PageList bsList(ScreeningBsVo entity, PageParam pageParam){
        List<ScreeningBsVo> bss = screeningBsService.queryListVo(entity, pageParam);
        Integer count = screeningBsService.queryTotalVo(entity);
        return new PageList(bss, count, count, pageParam.getsEcho());
    }

    @ApiOperation(value = "骨松筛查列表")
    @RequestMapping(value = "/blList", method = RequestMethod.POST)
    @ResponseBody
    public PageList blList(ScreeningBlVo entity, PageParam pageParam){
        List<ScreeningBlVo> bls = screeningBlService.queryListVo(entity, pageParam);
        Integer count = screeningBlService.queryTotalVo(entity);
        return new PageList(bls, count, count, pageParam.getsEcho());
    }

    @ApiOperation(value = "血压筛查结果")
    @RequestMapping(value = "/bpData", method = RequestMethod.POST)
    @ResponseBody
    public ApiRes bpData(Long id){
        ScreeningBpEntity entity = screeningBpService.queryObject(id);
        return new ApiRes(entity);
    }

    @ApiOperation(value = "血糖筛查结果")
    @RequestMapping(value = "/bsData", method = RequestMethod.POST)
    @ResponseBody
    public ApiRes bsData(Long id){
        ScreeningBsEntity entity = screeningBsService.queryObject(id);
        return new ApiRes(entity);
    }

    @ApiOperation(value = "骨松筛查结果")
    @RequestMapping(value = "/blData", method = RequestMethod.POST)
    @ResponseBody
    public ApiRes blData(Integer id){
        ScreeningBlEntity entity = screeningBlService.queryObject(id);
        return new ApiRes(entity);
    }

    @ApiOperation(value = "保存筛查筛查")
    @RequestMapping(value = "/saveBp", method = RequestMethod.POST)
    @ResponseBody
    public ApiRes saveBp(ScreeningBpEntity entity){
        screeningBpService.saveOrUpdate(entity);

        /**
         * 病人最后一次数据回显
         */
        PatientDataEntity data = new PatientDataEntity();
        data.setIdCard(entity.getIdCard());
        data.setHighPressure(entity.getHighPressure());
        data.setLowPressure(entity.getLowPressure());
        patientDataService.saveOrUpdate(data);
        return ApiRes.SUCCESS;
    }

    @ApiOperation(value = "保存血糖筛查")
    @RequestMapping(value = "/saveBs", method = RequestMethod.POST)
    @ResponseBody
    public ApiRes saveBs(ScreeningBsEntity entity){
        screeningBsService.saveOrUpdate(entity);

        PatientDataEntity data = new PatientDataEntity();
        data.setIdCard(entity.getIdCard());
        data.setBloodSugar(entity.getBloodSugarValue());
        patientDataService.saveOrUpdate(data);
        return ApiRes.SUCCESS;
    }

    @ApiOperation(value = "保存骨松筛查")
    @RequestMapping(value = "/saveBl", method = RequestMethod.POST)
    @ResponseBody
    public ApiRes saveBl(ScreeningBlEntity entity){
        screeningBlService.saveOrUpdate(entity);
        return ApiRes.SUCCESS;
    }

    @ApiOperation(value = "删除血压筛查")
    @RequestMapping(value = "/deleteBp", method = RequestMethod.POST)
    @ResponseBody
    public ApiRes deleteBp(Long[] ids){
        screeningBpService.deleteBatch(ids);
        return ApiRes.SUCCESS;
    }

    @ApiOperation(value = "删除血糖筛查")
    @RequestMapping(value = "/deleteBs", method = RequestMethod.POST)
    @ResponseBody
    public ApiRes deleteBs(Long[] ids){
        screeningBsService.deleteBatch(ids);
        return ApiRes.SUCCESS;
    }

    @ApiOperation(value = "删除骨松筛查")
    @RequestMapping(value = "/deleteBl", method = RequestMethod.POST)
    @ResponseBody
    public ApiRes deleteBl(Integer[] ids){
        screeningBlService.deleteBatch(ids);
        return ApiRes.SUCCESS;
    }

    @ApiOperation(value = "血压月度筛查")
    @RequestMapping(value = "/bpMonth", method = RequestMethod.POST)
    @ResponseBody
    public ApiRes bpMonth(@ApiParam(required = true, value = "时间(yyyy-MM)", name = "reportTime")
                            @RequestParam(value = "reportTime")String reportTime,
                          @ApiParam(required = true, value = "身份证", name = "idCard")
                            @RequestParam(value = "idCard")String idCard){
        ScreeningBpEntity entity = screeningBpService.monthReport(reportTime, idCard);
        return new ApiRes(entity);
    }

    @ApiOperation(value = "血糖月度筛查")
    @RequestMapping(value = "/bsMonth", method = RequestMethod.POST)
    @ResponseBody
    public ApiRes bsMonth(@ApiParam(required = true, value = "时间(yyyy-MM)", name = "reportTime")
                           @RequestParam(value = "reportTime")String reportTime,
                          @ApiParam(required = true, value = "身份证", name = "idCard")
                           @RequestParam(value = "idCard")String idCard){
        ScreeningBsEntity entity = screeningBsService.monthReport(reportTime, idCard);
        return new ApiRes(entity);
    }

}
